package kamikaze.service.setup;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import kamikaze.entity.Admin;
import kamikaze.entity.Order;
import kamikaze.entity.Order.PaymentStatus;
import kamikaze.entity.OrderItem;
import kamikaze.entity.Product;
import kamikaze.entity.User;
import kamikaze.support.HttpMethod;
import kamikaze.support.Service;
import kamikaze.support.ServiceConfig;
import kamikaze.support.ServiceContext;
import kamikaze.support.ServiceException;
import kamikaze.support.StringUtil;

@ServiceConfig(path = "setup", method = HttpMethod.GET)
public class SetupService implements Service {

	@Override
	public String serve(ServiceContext ctx) throws Exception {
		EntityManager em = ctx.getEntityManager();

		if (ctx.getRequest().getParameter("order") != null) {
			createSampleOrderList(em);
		}

		User user = null;
		Query query = em.createQuery("select count(u) from User u where loginId = :loginId");
		query.setParameter("loginId", "user");
		int total = ((Number) query.getSingleResult()).intValue();
		if (total == 0) {
			user = new User();
			user.setCardName("User");
			user.setCardNumber("5500000000000004");
			user.setCardType(User.CardType.MASTERCARD);
			user.setExpiredDate(new Date());
			user.setLoginId("user");
			user.setName("Default User");
			user.setPassword(StringUtil.encrypt("4klykjhs04"));
			ctx.getEntityManager().persist(user);
			ctx.getResponse().getWriter().print("created user default user.<br/>");

		} else {
			Query q = em.createQuery("select u from User u where loginId = :loginId");
			q.setParameter("loginId", "user");
			user = (User) q.getSingleResult();
		}

		em.getTransaction().commit();

		em.getTransaction().begin();

		Product product = new Product();

		product.setCategory("TEST");
		product.setComment("no");
		product.setDeleted(false);
		product.setName("TEST");
		product.setOwnerKey(user.getId());
		product.setOwnerName(user.getName());
		product.setPicture("");
		product.setPrice(BigDecimal.valueOf(100));
		product.setProductId(System.currentTimeMillis());
		product.setCreatedDate(new Date());

		ctx.getEntityManager().persist(product);

		ctx.getResponse().getWriter().print("created new product TEST<br/>");

		em.getTransaction().commit();

		em.getTransaction().begin();

		if (((Number) em.createQuery("select count(u) from Admin u").getSingleResult()).intValue() == 0) {
			em.persist(new Admin("admin", StringUtil.encrypt("4klykjhs04"), "admin"));
			ctx.getResponse().getWriter().print("created user admin:4klykjhs04<br/>");
		}
		em.getTransaction().commit();

		ctx.getResponse().getWriter().print("done");

		return null;
	}

	private void createSampleOrderList(EntityManager em) {

		List<User> list = em.createQuery("select u from User u").setMaxResults(10).getResultList();

		if (list.isEmpty()) {
			throw new ServiceException("no user");
		}

		Product product = (Product) em.createQuery("select p from Product p").setMaxResults(1).getSingleResult();

		for (User user : list) {

			Order order = new Order();

			order.setDescription("");

			order.addItem(new OrderItem("LAPTOP", BigDecimal.valueOf(100), 1, product.getProductId()));
			order.addItem(new OrderItem("LAPTOP", BigDecimal.valueOf(100), 1, product.getProductId()));

			order.setTotalPrice(new BigDecimal("24.25"));

			order.setUserId(user.getId());
			order.setStatus(PaymentStatus.SUCCESFUL);

			em.persist(order);

		}

	}
}
